
import numpy as np

def gauss_jordan_independent(m, debug=False, epsilon = 0.00001):
	n_rows = len(m)
	n_cols = m.size / len(m)
	m = m.astype('float_')		# copy
	if debug:
		print(m)
	curr_row = 0
	for curr_col in range(0, n_cols):
		if curr_row >= n_rows:
			break
		# select row with max curr_col value, down from curr_row
		max_row_index = abs(m[curr_row:]).argmax(0)[0, curr_col] + curr_row
		swap_rows(m, max_row_index, curr_row)

		if abs(m[curr_row, curr_col]) < epsilon:
			continue			# we choose next col, this one is already zeroed down from curr_row
		# reduce all rows down from curr_row
		m[curr_row] = m[curr_row] / m[curr_row, curr_col]
		for k in range(curr_row + 1, n_rows):
			m[k] = m[k] - m[curr_row] * m[k, curr_col]
		for k in range(0, curr_row):
			m[k] = m[k] - m[curr_row] * m[k, curr_col]
		curr_row = curr_row + 1
	if debug:
		print("Matrix after GJ elimination:")
		print(m)
	n_zero_rows = (abs(m).max(1) < epsilon).sum()
	if n_zero_rows > 0:
		return False
	else:
		return True

def swap_rows(matrix, i, j):
	temp_row = matrix[i].copy()
	matrix[i] = matrix[j]
	matrix[j] = temp_row
